<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Account/classes/PhpFox_ComponentChangePassword.class.php, $Author:  $ */



/** Change password compoenent.
 *
 * Parameters:
 *  1. oUser (mandatory) - user account object
 *  2. sTemplate (optional) - module template ('ChangePassword' by default)
 *
 * @package module.account
 * @version $Id: PhpFox_ComponentChangePassword.class.php 46 2007-12-14 23:12:21Z phpfoxsv $
 *
 */
class PhpFox_Mod_Account_ComponentChangePassword extends BaseComponent
{

    /** Processing method
     */
	function process()
	{
	    $oAcc = $this->getParam('oUser');
	    /* @var $oAcc PhpFox_Mod_Account_ItemAccount */
	    assert($oAcc != null);

	    $aErrors = array();

	    //validator
        $aFields = array(
        	'pass_old'  => array('title' => App::format('Mod_Account.old_password'), 'def' => 'required'),
        	'pass_new'  => array('title' => App::format('Mod_Account.new_password'), 'def' => 'password'),
        	'pass_new2' => array('title' => App::format('Mod_Account.confirm_new_password'), 'def' => 'password'),
        );
        $aRules = array(
        	array('pass_new', 'pass_new2', '==')
        );
        $oValidator = App::newObject('Validator', $aFields, $aRules);
        /* @var $oValidator Validator */

	    $oReq = &App::getRequest();
	    /* @var $oReq Request */

	    // processing
        switch($oReq->getAction())
        {
            case 'update':
				if ($oValidator->isValid($oReq->getAll()))
				{
					if ((App::pluginInstalled('vbulletin') && vbulletin_check_password($oReq->get('pass_old'))) || ($oAcc->aData['password'] == md5($oReq->get('pass_old'))))
					{
					    $oAcc->aData['password'] = md5($oReq->get('pass_new'));
    				    if ($oAcc->update(array('password')))
    				    {
    				        $aCallbackParams = array(
			        			'Account' => $oAcc,
			        		);
					        BaseService::callback($this, 'changepassword', $aCallbackParams);

					        App::plugin_changePassword(array($oAcc->aData['id'], $oReq->get('pass_new')));

							$oSecurity = $this->_oModule->getService('Security');

					        $oSecurity->login($oAcc->aData['user'], $oReq->get('pass_new'), $oAcc->aData['type'], $oAcc->aData['remember_me']);

    				        $oUrl = &App::getUrl();
    				        App::forward($oUrl->getUrl(), array(App::format('Mod_Account.password.updated')),
    				                     'message', App::getPageName(), 'ChangePassword');
    				    }
					}
                    else
                    {
                         $aErrors[] = App::format('Mod_Account.error.wrong_old_pass');
                    }
				}
				else
				{
				    $aErrors = $oValidator->getErrors();
				}

				break;
        }

		$oTpl = &$this->_oModule->getTemplate();
		$oTpl->assignSrc(array(
		    'aUser'        => $oAcc->aData,
			'jsValidation' => $oValidator->makeJS(),
			'jsValMess'    => $oValidator->makeJsMess(),
			'aErrors'      => $aErrors,

		));
		$sTemplate = $this->getParam('sTemplate', 'ChangePassword');

		$srcHtml = $oTpl->fetch($sTemplate.'.html');
		return $srcHtml;
	}

}
?>